﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class regUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserInfo"] != null)
                {
                    Response.Redirect("~/default.aspx");
                }
                ibUserOK.Attributes.Add("OnClick", "return testUserInput();");
                btnSchoolOK.Attributes.Add("OnClick", "return testSchoolInput();");
            }
        }

        //protected void MenuReg_MenuItemClick(object sender, MenuEventArgs e)
        //{
        //    for (int i = 0; i < MenuReg.Items.Count; i++)
        //    {
        //        if (i.ToString() == e.Item.Value)
        //        {
        //            MultiViewReg.ActiveViewIndex = i;
        //        }
        //    }
        //}

        protected void btnUserOK_Click(object sender, EventArgs e)
        {

            if (SQLHelper.getAccountCountByUserName(tbxUserName.Text) > 0)
            {
                Response.Write("<script>alert('该用户名已注册');location.href='regUser.aspx';</script>");
                return;
            }
            if (SQLHelper.getUserAccountCountByEmail(tbxUserEmail.Text) > 0)
            {
                Response.Write("<script>alert('该邮箱已注册');location.href='regUser.aspx';</script>");
                return;
            }

            int iresult = SQLHelper.RegUserAccount(tbxUserName.Text, tbxUserPassword.Text, tbxUserEmail.Text);
            if (iresult != 1)
                Response.Write("<script>alert('注册失败!');location.href='regUser.aspx';</script>");
            else
                Response.Write("<script>alert('注册成功!');location.href='Login.aspx';</script>");
        }

        protected void btnSchoolOK_Click(object sender, EventArgs e)
        {

            if (SQLHelper.getAccountCountByUserName(tbxSchoolName.Text) > 0)
            {
                Response.Write("<script>alert('该用户名已注册');location.href='regUser.aspx';</script>");
                return;
            }
            if (SQLHelper.getSchoolAccountCountByEmail(tbxSchoolEmail.Text) > 0)
            {
                Response.Write("<script>alert('该邮箱已注册');location.href='regUser.aspx';</script>");
                return;
            }

            int iresult = SQLHelper.RegSchoolAccount(tbxSchoolName.Text, tbxSchoolPassword.Text, tbxSchoolEmail.Text);
            if (iresult != 1)
                Response.Write("<script>alert('注册失败!');location.href='regUser.aspx';</script>");
            else
                Response.Write("<script>alert('注册成功!');location.href='Login.aspx';</script>");
        }
    }
}